﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using appDataContracts;

namespace appServices
{
    class Admin : appInterfaces.IAdmin
    {
        public SerializedAdmin adminLogin(string mail, string password)
        {
            Database db = Database.getInstance();

            try
            {
                ADMIN selectAdmin = (ADMIN)db.check(db.getModel().ADMIN.
                First(adm => adm.ADMINMAIL.Equals(mail) && adm.ADMINPASSWORD.Equals(password)));

                return new SerializedAdmin(
                    selectAdmin.ADMINID,
                    selectAdmin.ADMINMAIL,
                    selectAdmin.ADMINPASSWORD
                    );
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public List<SerializedSupplier> getSuppliers()
        {
            Database db = Database.getInstance();

            List<SerializedSupplier> listSuppliers = new List<SerializedSupplier>();
            var selectSuppliers = db.check(db.getModel().SUPPLIER.Select(sup => sup));

            foreach (SUPPLIER supplier in selectSuppliers)
            {
                listSuppliers.Add(Library.getSerializedSupplier(supplier));
            }

            return listSuppliers;
        }

        public bool addSupplier(SerializedSupplier supplier)
        {
            Database db = Database.getInstance();

            try
            {
                SUPPLIER newSupplier = new SUPPLIER();

                newSupplier.SUPPLIERADRESS = supplier.SUPPLIERADRESS;
                newSupplier.SUPPLIERCITY = supplier.SUPPLIERCITY;
                newSupplier.SUPPLIERCOUNTRY = supplier.SUPPLIERCOUNTRY;
                newSupplier.SUPPLIERDATELIMIT = supplier.SUPPLIERDATELIMIT;
                newSupplier.SUPPLIERMAIL = supplier.SUPPLIERMAIL;
                newSupplier.SUPPLIERNAME = supplier.SUPPLIERNAME;
                newSupplier.SUPPLIERPASSWORD = supplier.SUPPLIERPASSWORD;
                newSupplier.SUPPLIERPHONE = supplier.SUPPLIERPHONE;
                newSupplier.SUPPLIERZIP = supplier.SUPPLIERZIP;

                db.getModel().AddToSUPPLIER(newSupplier);
                db.getModel().SaveChanges();

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
        }

        public bool updateSupplier(SerializedSupplier supplier)
        {
            Database db = Database.getInstance();

            try
            {
                SUPPLIER supplierUpdate = (SUPPLIER)db.check(db.getModel().SUPPLIER.
                    First(sup => sup.SUPPLIERID == supplier.SUPPLIERID));

                supplierUpdate.SUPPLIERADRESS = supplier.SUPPLIERADRESS;
                supplierUpdate.SUPPLIERCITY = supplier.SUPPLIERCITY;
                supplierUpdate.SUPPLIERCOUNTRY = supplier.SUPPLIERCOUNTRY;
                supplierUpdate.SUPPLIERDATELIMIT = supplier.SUPPLIERDATELIMIT;
                supplierUpdate.SUPPLIERMAIL = supplier.SUPPLIERMAIL;
                supplierUpdate.SUPPLIERNAME = supplier.SUPPLIERNAME;
                supplierUpdate.SUPPLIERPASSWORD = supplier.SUPPLIERPASSWORD;
                supplierUpdate.SUPPLIERPHONE = supplier.SUPPLIERPHONE;
                supplierUpdate.SUPPLIERZIP = supplier.SUPPLIERZIP;

                db.getModel().SaveChanges();

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
        }

        public bool deleteSupplier(int supplierId)
        {
            try
            {
                Database db = Database.getInstance();

                SUPPLIER supplier = (SUPPLIER)db.check(db.getModel().SUPPLIER.
                        First(sup => sup.SUPPLIERID == supplierId));

                db.getModel().DeleteObject(supplier);
                db.getModel().SaveChanges();

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
        }
    }
}
